import Vue from 'vue'
import VueRouter from 'vue-router'
import index from '../views/Index/index'
import course from "../components/course";
import courseDetial from "../views/courseDetial/courseDetial";
import personal from "../views/personal/personal";
import store from "../store/index"
import {personalModel} from "../../api/login";
Vue.use(VueRouter)

const routes = [
  {
    path:"/index",
    component:index
  },
  {
    path:"/",
    redirect:"/index"
  },
  {
    path:"/course",
    component: course
  },
  {
    path:"/courseDetial",
    component: courseDetial
  },
  {
    path:"/personal",
    component: personal,
    meta:{
      needLogin:true
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next)=>{
  // console.log(to)
  // console.log(from)
  // console.log(next)
   if(to.meta.needLogin==true) {
     console.log(store.state.flag2);
     personalModel().then(res=>{
       if(res.code==0){
         next()
       }else{
         next({path:"/index",query:{dialogVisible:true}})
       }
     })

     // if(store.state.flag2==true) {
     //   console.log(1111)
     //
     // }else{
     //   console.log(222)
     //
     //   console.log(store.state.flag2)
     // }
   }else{
     console.log(333)
     next()
   }
})

export default router
